﻿using System;
using System.ComponentModel;
using System.Windows.Input;
using DeepEarth.Map.Core;
using DeepEarth.Map.Core.Utilities;
using DeepEarth.Core.Utilities;
using DeepEarth.Map.Core.Tiling;

namespace DeepEarth.Toolkit.Controls
{
	public sealed class OverlayModeViewModel : INotifyPropertyChanged, IDisposable
	{
		public MapMode Specification { get; private set; }

		private ICommand _selectedCommand;
		public ICommand SelectedCommand
		{
			get { return _selectedCommand; }
		}


		private bool _isChecked;
		public bool IsChecked
		{
			get { return _isChecked; }
			set
			{
				if (value == _isChecked) return;
				_isChecked = value;
				OnPropertyChanged("IsChecked");
			}
		}

		public OverlayModeViewModel(MapMode mode, BaseTileSourceProvider overlayMode)
		{
			Specification = mode;
			_selectedCommand = new LambdaCommand(() =>
			                                     {
			                                     	var currentIsChecked = !IsChecked;
			                                     	if (currentIsChecked)
			                                     		overlayMode.AddTileSourceByMode(Specification);
			                                     	else
			                                     		overlayMode.RemoveTileSourceBySpecification(Specification);
			                                     	IsChecked = currentIsChecked;
			                                     });
		}

		public event PropertyChangedEventHandler PropertyChanged;
		private void OnPropertyChanged(string propertyName)
		{
			var evt = PropertyChanged;
			if (evt != null)
				evt(this, new PropertyChangedEventArgs(propertyName));
		}

		public void Dispose()
		{
			_selectedCommand = null;
		}
	}
}
